﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Fuse8.DomainFramework.Common
{
	internal static class TypeExtensions
	{
		public static bool IsNullableEnum (this Type type)
		{
			return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>) && type.GetGenericArguments().Count() == 1 && type.GetGenericArguments().First().IsEnum;
		}		
		
		public static bool IsNullable (this Type type)
		{
			return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
		}

        public static Type GetNullableTypeParameter(this Type type)
        {
            if (type.IsNullable())
            {
                return type.GetGenericArguments()[0];
            }
            return null;
        }

		public static bool IsEnumerable (this Type type)
		{
			return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>);
		}
	}
}